/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.sober.config;

import com.jspx.utils.StringUtil;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import java.io.CharArrayWriter;
import java.util.Map;
import java.util.HashMap;
import com.jspx.utils.XMLUtil;


/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2007-1-6
 * Time: 23:58:45
 */
public class SoberSQLMapXML extends DefaultHandler
{
    private CharArrayWriter contents = new CharArrayWriter();
    private MapSQL querySQL = null;
    private MapSQL updateSQL = null;
    private MapSQL executeSQL = null;
    private Map<String, SQLRoom> allSQLMap = null;
    private String namespace;

    public SoberSQLMapXML(Map<String, SQLRoom> allSQLMap)
    {
        if (allSQLMap == null)
        {
            allSQLMap = new HashMap<String, SQLRoom>();
        }
        this.allSQLMap = allSQLMap;
    }

    public void startElement(String namespaceURI,
                             String localName,
                             String qName,
                             Attributes attr) throws SAXException
    {
        contents.reset();
        if (localName.equalsIgnoreCase("sqlmap"))
        {
            namespace = attr.getValue("namespace");
        }
        if (localName.equalsIgnoreCase("query")|| localName.equalsIgnoreCase("select"))
        {
            SQLRoom sqlRoom = allSQLMap.get(namespace);
            if (sqlRoom == null)
            {
                sqlRoom = new SQLRoom();
                allSQLMap.put(namespace, sqlRoom);
            }
            querySQL = new MapSQL();
            querySQL.setId(attr.getValue("id"));

            String resultType = attr.getValue("class");
            if (StringUtil.isNULL(resultType)) resultType = attr.getValue("resultType");
            querySQL.setResultClass(resultType);
            querySQL.setCache(StringUtil.toBoolean(attr.getValue("cache")));
            sqlRoom.getQueryMap().put(querySQL.getId(), querySQL);
        }
        if (localName.equalsIgnoreCase("update"))
        {
            SQLRoom sqlRoom = allSQLMap.get(namespace);
            if (sqlRoom == null)
            {
                sqlRoom = new SQLRoom();
                allSQLMap.put(namespace, sqlRoom);
            }
            updateSQL = new MapSQL();
            updateSQL.setId(attr.getValue("id"));

            String resultType = attr.getValue("class");
            if (StringUtil.isNULL(resultType)) resultType = attr.getValue("resultType");
            updateSQL.setResultClass(resultType);
            sqlRoom.getUpdateMap().put(updateSQL.getId(), updateSQL);
        }
        if (localName.equalsIgnoreCase("execute"))
        {
            SQLRoom sqlRoom = allSQLMap.get(namespace);
            if (sqlRoom == null)
            {
                sqlRoom = new SQLRoom();
                allSQLMap.put(namespace, sqlRoom);
            }
            executeSQL = new MapSQL();
            executeSQL.setId(attr.getValue("id"));

            String resultType = attr.getValue("class");
            if (StringUtil.isNULL(resultType)) resultType = attr.getValue("resultType");
            executeSQL.setResultClass(resultType);

            sqlRoom.getExecuteMap().put(executeSQL.getId(), executeSQL);
        }
    }

    public void endElement(String namespaceURI,
                           String localName,
                           String qName) throws SAXException
    {
        if (localName.equalsIgnoreCase("query")|| localName.equalsIgnoreCase("select"))
        {
            querySQL.setContext(XMLUtil.escapeDecrypt(contents.toString().trim()));
        }
        if (localName.equalsIgnoreCase("update"))
        {
            updateSQL.setContext(XMLUtil.escapeDecrypt(contents.toString().trim()));
        }
        if (localName.equalsIgnoreCase("execute"))
        {
            executeSQL.setContext(XMLUtil.escapeDecrypt(contents.toString().trim()));
        }    
    }

    public void characters(char[] ch, int start, int length)
            throws SAXException
    {
        contents.write(ch, start, length);
    }

    public Map<String, SQLRoom> getSQLMap()
    {
        return allSQLMap;
    }
}